package Tests
{
	import Box2D.Collision.Shapes.b2EdgeShape;
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Common.b2Settings;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2BodyType;
	
	import Framework.StringUtil;
	import Framework.Test;
	
	import mx.messaging.events.MessageAckEvent;
	
	public class BulletTest extends Test
	{
		private var m_body:b2Body;
		private var m_bullet:b2Body;
		private var m_x:Number;
		public function BulletTest()
		{
			super();
			createGround();
			createBodies();
		}
		private function createGround():void{
			var edge:b2EdgeShape = new b2EdgeShape;
			edge.Set(new b2Vec2(-10,0),new b2Vec2(10,0));
			m_groundBody.CreateFixtureByShape(edge,0);
			
			var shape:b2PolygonShape = new b2PolygonShape;
			shape.SetAsBoxByCenter(.2,1,new b2Vec2(.5,1),0);
			m_groundBody.CreateFixtureByShape(shape,0);
		}
		private function createBodies():void{
			var bd:b2BodyDef = new b2BodyDef;
			bd.type = b2BodyType.b2_dynamicBody;
			bd.position.Set(0.0, 4.0);
			
			var box:b2PolygonShape = new b2PolygonShape;
			box.SetAsBox(2.0, 0.1);
			
			m_body = m_world.CreateBody(bd);
			m_body.CreateFixtureByShape(box, 1.0);
			
			box.SetAsBox(0.25, 0.25);
			
			//m_x = RandomFloat(-1.0f, 1.0f);
			m_x = 0.20352793;
			bd.position.Set(m_x, 10.0);
			bd.bullet = true;
			
			m_bullet = m_world.CreateBody(bd);
			m_bullet.CreateFixtureByShape(box, 100.0);
			
			m_bullet.SetLinearVelocity(new b2Vec2(0.0, -50.0));
			
		}
		private function Launch():void
		{
			m_body.SetTransform(new b2Vec2(0.0, 4.0), 0.0);
			m_body.SetLinearVelocity(new b2Vec2());
			m_body.SetAngularVelocity(0.0);
			
			m_x = RandomFloat(-1.0, 1.0);
			m_bullet.SetTransform(new b2Vec2(m_x, 10.0), 0.0);
			m_bullet.SetLinearVelocity(new b2Vec2(0.0, -50.0));
			m_bullet.SetAngularVelocity(0.0);
			
			
			b2Settings.b2_gjkCalls = 0;
			b2Settings.b2_gjkIters = 0;
			b2Settings.b2_gjkMaxIters = 0;
			
			b2Settings.b2_toiCalls = 0;
			b2Settings.b2_toiIters = 0;
			b2Settings.b2_toiMaxIters = 0;
			b2Settings.b2_toiRootIters = 0;
			b2Settings.b2_toiMaxRootIters = 0;
		}
		public override function Step():void{
			super.Step();
			var msg:String;
			if(b2Settings.b2_gjkCalls>0){
				msg =  StringUtil.sprintf("gjk calls = %d, ave gjk iters = %3.1f, max gjk iters = %d",
					b2Settings.b2_gjkCalls, b2Settings.b2_gjkIters / Number(b2Settings.b2_gjkCalls), b2Settings.b2_gjkMaxIters);
				writeMessage(msg);				
			}
			if (b2Settings.b2_toiCalls > 0)
			{
				msg =  StringUtil.sprintf("toi calls = %d, ave toi iters = %3.1f, max toi iters = %d",
					b2Settings.b2_toiCalls, b2Settings.b2_toiIters / Number(b2Settings.b2_toiCalls), b2Settings.b2_toiMaxRootIters);
				writeMessage(msg);				
				
				msg =  StringUtil.sprintf("ave toi root iters = %3.1f, max toi root iters = %d",
					b2Settings.b2_toiRootIters / Number(b2Settings.b2_toiCalls), b2Settings.b2_toiMaxRootIters);
				writeMessage(msg);				
			}			
			
			if (m_stepCount % 60 == 0)
			{
				Launch();
			}
		}
	}
}